﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace IUploading
{
    public class TransferResponsePackge
    {
        /// <summary>
        /// 用于存储输出参数的动态对象
        /// </summary>
        private JObject _params = new JObject();

        /// <summary>
        /// 接口名称
        /// </summary>
        public string InterfaceName
        {
            get;
            set;
        }

        /// <summary>
        /// 执行的传输动作名称
        /// </summary>
        public string Action
        {
            get;
            set;
        }

        /// <summary>
        /// 执行动作的结果
        /// </summary>
        public TransferResult Result 
        {
            get;
            set;
        }

        /// <summary>
        ///执行动作有关的消息(通常指向错误信息，或者异常信息，比如执行失败的原因)
        /// </summary>
        public string Message
        {
            get;
            set;
        }

        /// <summary>
        /// 执行业务功能需求的动作是否执行成功
        /// 例如读取信息是否读到有效信息，读到为False
        /// 直到结束都未读到，或者读取超时应设为False
        /// </summary>
        public bool Success
        {
            get;
            set;
        }

        /// <summary>
        /// 方法返回的调用结果，通常是一个枚举，Int
        /// </summary>
        public int Code
        {
            get;
            set;
        }

        /// <summary>
        /// 输入参数，通常和_params作为联动对象出现
        /// </summary>
        public string OutParams
        {
            get
            {
                return _params.ToString();
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    _params = new JObject();
                }
                else
                {
                    _params = JObject.Parse(value);
                }
            }
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public TransferResponsePackge()
        {

        }

        /// <summary>
        /// 只指定调用类型和调用方法名称
        /// </summary>
        /// <param name="sub"></param>
        /// <param name="fun"></param>
        public TransferResponsePackge(string name, string action)
        {
            InterfaceName = name;
            Action = action;
        }

        /// <summary>
        /// 将Json字符串转化为Json对象
        /// </summary>
        public void SetParams(string jsonString)
        {
            OutParams = jsonString;           
        }

        public void Add(string key, string value)
        {
            if (_params == null)
            {
                _params = new JObject();
            }
            _params[key] = value;
        }

        /// <summary>
        /// 获取或者设置输出参数选项
        /// </summary>
        /// <param name="key">0，表示输入参数；1(!=0)，表示输出参数</param>
        /// <param name="value"></param>
        /// <returns></returns>
        [JsonIgnore]
        public string this[string key]
        {
            get
            {
                if (_params == null)
                {
                    return "";
                }
                try
                {                 
                    // 欲获取输出参数
                    return _params[key].ToString();
                }
                catch
                {
                    return "";
                }              
            }
            set
            {
                Add(key, value);
            }
        }
    }
}
